//
//  VIZHomeViewModel.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/13.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit

class VIZHomeViewModel: NSObject {
    
    //模型数组
    var cellViewModelArray : [VIZCellViewModel] = [VIZCellViewModel]()
    
    func loadHomeData(finished:(isSuccess:Bool)->()) {
        
        guard let token = VIZUserViewAccountModel.shardInstance.userInfo?.access_token else {
            print("用户没有登录")
            return
        }
        
        VIZAFNetWorkTools.shardTools.loadHomePageData(token) { (result, error) -> () in
            if error != nil{
                print(error)
                //SVProgressHUD.showErrorWithStatus("首页数据没有得到啊")
                return
            }
            //print("==============================")
            //print(result)
            //将result转为字典类型
            guard let dict = result as? [String : AnyObject] else {
                //SVProgressHUD.showErrorWithStatus("数据格式错误啊")
                finished(isSuccess: false)
                return
            }
            //读取字典并转模型,获得statusArray
            guard let statusArray = dict["statuses"] as? [[String:AnyObject]] else {
                //SVProgressHUD.showErrorWithStatus("数据格式错误啊")
                finished(isSuccess: false)
                return
            }
            //读取statusArray,并存到模型中去
            //创建可变数组
            var homeStatusModelList = [VIZCellViewModel]()
            //遍历statusArray,转模型
            for item in statusArray{
                let homeStatues = VIZHomeStatuse(dict: item)
                let cellViewModel = VIZCellViewModel(homeStatuse: homeStatues)
                //print(homeStatues.screen_name)
                homeStatusModelList.append(cellViewModel)
            }
            //保存数组
            self.cellViewModelArray = homeStatusModelList
            //刷新数据
            //self.tableView.reloadData()
            finished(isSuccess: true)
        }

        
        
    }
    
    
    
    
    
    
    

}
